#include "test.h"

#include "serialportsync.h"
#include "u.h"

Test::Test(QObject *parent)
    : QObject{parent},m_name("默认名字")
{
    qDebug() <<"Test对象创建" << this;
    QObject::connect (this, &Test::signalTest, this, [&]{
        qDebug() <<this << name() << "信号发射";
    });
    QObject::connect (this, &Test::nameChanged, this, [&]{
        qDebug() <<this << "更名成为"<<m_name;
    });
    m_size = 5;
}

void Test::normalMethod()
{
    qDebug() <<this << name() << "normalMethod invoke";
}

QString Test::getTemperature(const QString &data)
{
    qDebug() <<this << name() << data;
    SerialPortSync serial;
    QByteArray response;
    // 打开串口，根据实际情况修改参数
    if (serial.open("COM20")) { // Windows系统通常是COMx，Linux是/dev/ttyUSBx等
        qDebug() << "串口打开成功";
        QByteArray sendData=U::hexStringToByteArray(data);
        qDebug()<< "start to send data->" <<data;
        response = serial.sendSync(sendData); // 5秒超时

        if (!response.isEmpty()) {
            qDebug() << "收到响应<- " << response.toHex();
        } else {
            qDebug() << "未收到响应或超时";
        }
        serial.close();
    } else {
        qDebug() << "串口打开失败: " ;
    }
    return U::byteArrayToHexString(response,true, true);

}

QString Test::getWeight(const QString &data)
{
    qDebug() <<this << name() << "Q_INVOKABLE method invoke";
    SerialPortSync serial;
    QByteArray response;
    // 打开串口，根据实际情况修改参数
    if (serial.open("COM18")) { // Windows系统通常是COMx，Linux是/dev/ttyUSBx等
        qDebug() << "串口打开成功";
        QByteArray sendData=U::hexStringToByteArray(data);
        qDebug()<< "start to send data->" <<data;
        response = serial.sendSync(sendData); // 5秒超时

        if (!response.isEmpty()) {
            qDebug() << "收到响应<- " << response.toHex();
        } else {
            qDebug() << "未收到响应或超时";
        }
        serial.close();
    } else {
        qDebug() << "串口打开失败: " ;
    }
    return U::byteArrayToHexString(response,true, true);
}

QString Test::calibrationgWeight()
{
    SerialPortSync serial;
    QByteArray response;
    // 打开串口，根据实际情况修改参数
    if (serial.open("COM18")) { // Windows系统通常是COMx，Linux是/dev/ttyUSBx等
        qDebug() << "串口打开成功";
        sendData(serial, "0A 06 00 06 00 00 68 B0");
        sendData(serial, "0A 06 00 06 00 01 A9 70");
        QThread::sleep(12);
        sendData(serial, "0A 06 00 06 00 00 68 B0");
        sendData(serial, "0A 06 00 09 00 01 99 73");
        sendData(serial, "0A 06 00 06 00 03 28 B1");
        QThread::sleep(12);
        serial.close();
    } else {
        qDebug() << "串口打开失败: " ;
    }
    return "";
}

QString Test::getPH(const QString &data)
{
    qDebug() <<this << name() << "Q_INVOKABLE method invoke";
    SerialPortSync serial;
    QByteArray response;
    // 打开串口，根据实际情况修改参数
    if (serial.open("COM17", QSerialPort::Baud19200)) { // Windows系统通常是COMx，Linux是/dev/ttyUSBx等
        qDebug() << "串口打开成功";
        QByteArray sendData=U::hexStringToByteArray(data);
        qDebug()<< "start to send data->" <<data;
        response = serial.sendSync(sendData); // 5秒超时

        if (!response.isEmpty()) {
            qDebug() << "收到响应<- " << response.toHex();
        } else {
            qDebug() << "未收到响应或超时";
        }
        serial.close();
    } else {
        qDebug() << "串口打开失败: " ;
    }
    return U::byteArrayToHexString(response,true, true);
}

float Test::byteToFloat(const QString &data)
{
    QByteArray arr=U::hexStringToByteArray(data);
    return  U::hexToFloat(arr, true);
}

float Test::byteToFloatFlip(const QString &data)
{
    QByteArray a=U::hexStringToByteArray(data);
    QByteArray b;
    b.append(a.mid(2,2));
    b.append(a.mid(0,2));

    return  U::hexToFloat(b, true);
}

void Test::testCRC()
{
    QVector<uint8_t> data = {0x01, 0x10, 0x01, 0x12, 0x00, 0x02, 0x04,0x00, (char)0x00,0x00,0x00};
    uint16_t crc = U::getCRC(data);
    uint8_t highByte = (crc >> 8) & 0xFF;
    uint8_t lowByte = crc & 0xFF;
    QString hexStr = QString("%1->%2:%3").arg(crc, 0, 16).arg(highByte,0,16).arg(lowByte,0,16);
    qDebug() <<hexStr;
}

void Test::testFloat()
{
    QByteArray arr=QByteArray::fromHex("40A051EC");
    float f = U::hexToFloat(arr, true);
    qDebug()<< "f:" << f;

    arr=QByteArray::fromHex("3F7B7CEF");
    f = U::hexToFloat(arr,true);
    qDebug()<< "f:" << f;

    arr=QByteArray::fromHex("40EB795A");
    f = U::hexToFloat(arr,true);
    qDebug()<< "f:" << f;

    arr=QByteArray::fromHex("41600000");
    f = U::hexToFloat(arr,true);
    qDebug()<< "f:" << f;
}

Test::~Test()
{
    qDebug() <<this << name() << "～Test method invoke";
}

QString Test::name() const
{
    return m_name;
}

void Test::setName(const QString &newName)
{
    if (m_name == newName)
        return;
    m_name = newName;
    emit nameChanged();
}

void Test::resetName()
{
    setName({}); // TODO: Adapt to use your actual default value
}

void Test::sendData(SerialPortSync &serialPort, const QString &data)
{
    QByteArray sendData=U::hexStringToByteArray(data);
    qDebug()<< "send data->" <<data;
    QByteArray response = serialPort.sendSync(sendData); // 5秒超时
    if (response.isEmpty()){
        qDebug() << "未收到响应或超时";
        return;
    }
    qDebug() << "receive data<- " << response.toHex();
    return;
}

void Test::slotsMethod()
{
    qDebug() <<this << name() << "slots Method invoke";
}
